package com.kedacom.freedomLabel.view.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.ProductTotalSelectLayoutBinding;
import com.kedacom.freedomLabel.model.bean.IMainSelectItem;
import com.lzscale.scalelib.misclib.Misc;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;

/**
 * @author licheng
 * @since 2020/3/14 15:59
 */
public class ProductTotalItemLayout extends LinearLayout implements ProductSelectItemView.DeleteSelectItemCallback {

    public static final String CATEGORY_PRODUCT = "CATEGORY_PRODUCT";
    public static final String CATEGORY_OPERATOR = "CATEGORY_OPERATOR";
    public static final String CATEGORY_SCAN = "CATEGORY_SCAN";
    Context mContext;
    ProductTotalSelectLayoutBinding mBinding;
    // 缓存数据
    Map<String, CacheData> mCacheItems = new HashMap<>();

    Misc mMisc = Misc.newInstance();

    public ProductTotalItemLayout(Context context) {
        this(context, null);
    }

    public ProductTotalItemLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProductTotalItemLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.product_total_select_layout, this, false);
        addView(mBinding.getRoot());
        mBinding.arrowUpward.setOnClickListener(v -> {
            mMisc.beep();
            mBinding.scroll.fullScroll(View.FOCUS_UP);
        });
        mBinding.arrowDownward.setOnClickListener(v -> {
            mMisc.beep();
            mBinding.scroll.fullScroll(View.FOCUS_DOWN);
        });
    }

    public boolean isItemReady() {
        boolean isReady = false;
        int childCount = mBinding.container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            ProductSelectItemView child = (ProductSelectItemView) mBinding.container.getChildAt(i);
            isReady = child.isReadySubmit();
            if (!isReady) break;
        }
        return isReady;
    }

    public Map<String, CacheData> getCacheItems() {
        int childCount = mBinding.container.getChildCount();
        Map<String, CacheData> data = new HashMap<>();
        for (int i = 0; i < childCount; i++) {
            ProductSelectItemView child = (ProductSelectItemView) mBinding.container.getChildAt(i);
            String category = child.getItemCategory();
            IMainSelectItem result = child.getResult();
            if (result != null && result.getSelectResultData() != null) {
                CacheData bean = new CacheData();
                bean.uuid = result.getSelectResultData()[0];
                bean.itemValue = result.getSelectResultData()[1];
                bean.value = result;
                data.put(category, bean);
            }
        }
        return data;
    }

    public void setItemViewResult(String category, String uuid, String itemValue, IMainSelectItem selectItem) {
        int childCount = mBinding.container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            ProductSelectItemView child = (ProductSelectItemView) mBinding.container.getChildAt(i);
            if (!category.equals(child.getItemCategory())) continue;
            child.setResult(itemValue, selectItem);
            CacheData cacheData = mCacheItems.get(category);
            if (cacheData == null) cacheData = new CacheData();
            cacheData.uuid = uuid;
            cacheData.itemValue = itemValue;
            cacheData.value = selectItem;
            mCacheItems.put(category, cacheData);
        }
    }

    public void clearAllSelectItemView() {
        mBinding.container.removeAllViews();
    }

    public void resetCacheItemValue() {
        int childCount = mBinding.container.getChildCount();
        for (int i = 0; i < childCount; i++) {
            ProductSelectItemView child = (ProductSelectItemView) mBinding.container.getChildAt(i);
            String category = child.getItemCategory();
            CacheData cacheData = mCacheItems.get(category);
            if (cacheData == null) continue;
            child.setResult(cacheData.itemValue, cacheData.value);
        }
    }

    public void addSelectItemView(String itemName, String category, Boolean isRequired, View.OnClickListener clickListener) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.topMargin = 5;
        params.bottomMargin = 5;
        ProductSelectItemView itemView = new ProductSelectItemView(mContext);
        itemView.setItemInformation(itemName, category, isRequired);
        itemView.setItemViewClickListener(clickListener);
        itemView.setDeleteSelectItemCallback(this);
        mBinding.container.addView(itemView, params);
        CacheData cacheData = mCacheItems.get(category);
        if (cacheData == null) cacheData = new CacheData();
        mCacheItems.put(category, cacheData);
    }

    @Override
    public void deleteSelectItem(String category) {
        mCacheItems.remove(category);
    }

    public static class CacheData {

        public String uuid;
        public String itemValue;
        public IMainSelectItem value;

    }
}